package top.lyc829.aircontroller.logic.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtils {

    private static final int SUCCESS = 200;


    public static void parseList(String json,CallBack callBack){
        try {
            JSONObject jsonObject = new JSONObject(json);
            int code = jsonObject.getInt("code");
            String msg = jsonObject.getString("msg");

            if (code != SUCCESS){
                callBack.returnInfo(HttpUtils.FAIL,msg,null);
                return;
            }

            List<String> listData = new ArrayList<>();
            JSONArray list = jsonObject.getJSONArray("data");

            for(int i=0;i<list.length();i++){
                listData.add(list.getString(i));
            }

            callBack.returnInfo(HttpUtils.SUCCESS,msg,listData);
        } catch (JSONException e) {
            callBack.returnInfo(HttpUtils.FAIL,null,null);
        }
    }

    public static void parseMap(String json, List<String> keys, CallBack callBack){
        try {
            JSONObject jsonObject = new JSONObject(json);
            int code = jsonObject.getInt("code");
            String msg = jsonObject.getString("msg");

            if (code != SUCCESS){
                callBack.returnInfo(HttpUtils.FAIL,msg,null);
                return;
            }

            List<Map<String,String>> listData = new ArrayList<>();
            JSONArray list = jsonObject.getJSONArray("data");

            for (int i=0;i<list.length();i++){
                Map<String,String> data = new HashMap<>();
                JSONObject object = (JSONObject) list.get(i);
                for (String key : keys){
                    String value = object.getString(key);
                    data.put(key,value);
                }
                listData.add(data);
            }

            callBack.returnInfo(HttpUtils.SUCCESS,msg,listData);
        } catch (JSONException e) {
            callBack.returnInfo(HttpUtils.FAIL,null,null);
        }
    }

    public static void parseBean(String json,Class clazz,CallBack callBack){
        try {
            JSONObject jsonObject = new JSONObject(json);
            int code = jsonObject.getInt("code");
            String msg = jsonObject.getString("msg");

            if (code != SUCCESS){
                callBack.returnInfo(HttpUtils.FAIL,msg,null);
                return;
            }

            Field[] fields = clazz.getDeclaredFields();
            List<Object> listData = new ArrayList<>();
            JSONArray jsonList = jsonObject.getJSONArray("data");

            for (int i=0;i<jsonList.length();i++){

                Object data = clazz.newInstance();
                JSONObject object = (JSONObject) jsonList.get(i);

                for (Field field : fields){
                    String type = field.getType().toString();
                    if (type.equals("class java.util.ArrayList")){
                        ArrayList<String> value = new ArrayList<>();
                        JSONArray list = null;
                        try{
                            list = object.getJSONArray(field.getName());
                        }catch (JSONException e){
                            continue;
                        }
                        for(int j=0;j<list.length();j++){
                            String s = list.getString(j);
                            value.add(s);
                        }
                        field.setAccessible(true);
                        field.set(data,value);
                    }else if (type.equals("class java.lang.String")){
                        String value;
                        try{
                            value = object.getString(field.getName());
                        }catch (JSONException e){
                            continue;
                        }
                        field.setAccessible(true);
                        field.set(data,value);
                    }
                }
                listData.add(data);
            }
            callBack.returnInfo(HttpUtils.SUCCESS,msg,listData);
        } catch (JSONException e) {
            callBack.returnInfo(HttpUtils.FAIL,null,null);
        } catch (IllegalAccessException e) {
            callBack.returnInfo(HttpUtils.FAIL,null,null);
        } catch (InstantiationException e) {
            callBack.returnInfo(HttpUtils.FAIL,null,null);
        }
    }



}
